/**
 * 
 */
package com.terrence.log.impl.log4j;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.terrence.log.AbsLogHandler;
import com.terrence.log.AbsLogger;
import com.terrence.log.LogException;
import com.terrence.log.LogLevel;

/**
 * @author wuchuang
 *
 * 2016年9月14日 下午2:32:16
 * 
 * Logger的log4j实现
 *   1.此实现依赖于Log4j及log4j.properties配置文件
 */
public class Logger4Log4j extends AbsLogger {
	
	private final static Logger log4j = Logger.getLogger(Logger4Log4j.class);
	
	private static Object lock = new Object();
	private static Logger4Log4j logger = null;
	
	private Logger4Log4j(){};
	
	public static Logger4Log4j getInstance(){
		if(logger == null){
			synchronized(lock){
				if(logger == null){
					logger = new Logger4Log4j();
				}
			}
		}
		logger.setLogLevelCode(LogLevel.ALL.getCode());
		setLogLevel();
		return logger;
	}
	
	/**
	 * 设置日志级别
	 * @throws LogException
	 */
	private static void setLogLevel() throws LogException{
		log4j.setLevel(Level.ALL);	
	}
	
	/**
	 * 
	 * 调试级别日志
	 * @param debug
	 * @throws LogException
	 */
	public void debug(String debug) throws LogException {
		super.debug(debug,new AbsLogHandler(){
			@Override
			public void handle(Object log, Throwable cause) throws LogException {
				log4j.setLevel(Level.DEBUG);
				log4j.debug(log);
			}});
	}

	/**
	 * 
	 * 信息级别日志
	 * @param info
	 * @throws LogException
	 */
	public void info(Object info) throws LogException {
		this.setLogLevelCode(LogLevel.INFO.getCode());
		super.info(info,new AbsLogHandler(){
			@Override
			public void handle(Object log, Throwable cause) throws LogException {
				log4j.setLevel(Level.INFO);
				log4j.info(log);
			}
		});
		
	}
	
	/**
	 * 
	 * 异常级别日志
	 * @param error
	 * @param cause
	 * @throws LogException
	 */
	public void error(Object error,Throwable cause) throws LogException {
		super.error(error, cause, new AbsLogHandler(){
			@Override
			public void handle(Object log, Throwable cause) throws LogException {
				log4j.setLevel(Level.ERROR);
				log4j.error(log, cause);	
			}
		});
	}
}
